package com.sgb.kjwl.viewmodel.shopping;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.shopping.ShopMyPriceListEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.shop.ShopMyPriceFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShopMyPriceModel extends BaseViewModel {

    private ShopMyPriceFragment fragment;
    private SelfRepertory repertory;
    private final int pageLimit = 10;
    private static final String TAG = "ShopMyPriceModel";
    public ShopMyPriceModel(ShopMyPriceFragment fragment,  SelfRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //查询我的报价列表
    public void getDataList(int status, String search,int pager) {
        DialogHelper.showProgressDialog(fragment.getActivity(),null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getPriceShop(status,pager,pageLimit,search).enqueue(new Callback<BaseEntity<ListResponse<List<ShopMyPriceListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ShopMyPriceListEntity>>>> call, Response<BaseEntity<ListResponse<List<ShopMyPriceListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getShopMyPriceDataList(response.body().getData());
                } else {
                    assert response.body() != null;
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
        }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ShopMyPriceListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }

        });
    }
    //查询详情数据
    public void getDataInfo(String note) {
//        ShopPriceDetailsAct.start(fragment.getActivity(),note);
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("pensonalQuoteDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!TextUtils.isEmpty(response.body().getData().getUrl())){
                        String url = response.body().getData().getUrl() + "?no=" + note;
                        showUrl(url);
                    }else {
                        showUrl("");
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
            }
        });
    }
//    跳转到H5界面
    public void showUrl(String url) {
        WebViewX5HostActivity.start(fragment.getActivity(), url);
      //  fragment.getActivity().finish();
    }
}
